module.service('Forest', ['$rootScope', function ($rootScope) {
    var service = {
        _baseUrl: "/azeroth/back/forest/",
        currentForest: {},
        forestList: [],
        forestTree: [],
        rootForestList: [],
        childForestList: [],
        forestPropertyList: [],
        skuPropertyValueMap: {},
        otherPropertyValueMap: {},
        totalSceneCount: 0,
        queryList: function (currentPage, pageSize, title, parentId, callback) {

            var paramData = {
                title: title,
                parentId: parentId,
                pageNum: currentPage,
                pageSize: pageSize
            };
            $rootScope.ajaxRequestPost(service._baseUrl + "getForestList.htm", paramData, function (resultJson) {
                angular.copy(resultJson.data, service.forestList);
                service.totalSceneCount = resultJson.totalCount;
                if (callback) {
                    callback();
                }
            });

        },
        getForestTree: function (callback) {
            var paramData = {};
            $rootScope.ajaxRequestPost(service._baseUrl + "getForestTree.htm", paramData, function (resultJson) {
                angular.copy(resultJson.data, service.forestTree);
                var _childForestList = [];
                var _rootForestList = [];
                $.each(service.forestTree, function (key, value) {
                    _rootForestList.push(value.parentDO);
                    _childForestList = _childForestList.concat(value.childList);
                });
                angular.copy(_childForestList, service.childForestList);
                angular.copy(_rootForestList, service.rootForestList);
                if (callback) {
                    callback();
                }
            });

        },
        queryForestDetail: function (forestId, callback) {

        },
        queryForestProperty: function (forestId, callback) {
            var paramData = {
                forestId: forestId
            };
            $rootScope.ajaxRequestPost("/azeroth/back/forestproperty/getForestPropertyList.htm", paramData, function (resultJson) {
                angular.copy(resultJson.data, service.forestPropertyList);
                if (callback) {
                    callback();
                }
            });
        },
        queryForestPropertyValue: function (forestId, callback) {
            var paramData = {
                forestId: forestId
            };
            $rootScope.ajaxRequestPost("/azeroth/api/propertyvalue/getForestPropertyValueList.htm", paramData, function (resultJson) {
                //这里要迭代json哦
                angular.copy(resultJson.data[0], service.skuPropertyValueMap);
                angular.copy(resultJson.data[1], service.otherPropertyValueMap);
                //service.skuPropertyValueMap.sort(function (a, b) {
                //    a.localeCompare(b);
                //});
                if (callback) {
                    callback();
                }
            });
        },
        insertForestPropertys: function (forestId, propertyIds, callback) {
            var paramData = {
                forestId: service.currentForest.id,
                propertyIds: propertyIds
            };
            $rootScope.ajaxRequestPost("/azeroth/back/forestproperty/publishForestPropertys.htm", paramData, function (resultJson) {
                angular.copy(resultJson.data, service.forestPropertyList);
                if (callback) {
                    callback();
                }
            });
        },
        removeForestProperty: function (id, callback) {
            var paramData = {
                id: id
            };
            $rootScope.ajaxRequestPost("/azeroth/back/forestproperty/remove.htm", paramData, function (resultJson) {

                //根据id删除掉该数据
                var removeIndex;
                $.each(service.forestPropertyList, function (index, result) {
                    if (result['id'] == id) {
                        removeIndex = index;
                    }
                });
                service.forestPropertyList.splice(removeIndex, 1);

                if (callback) {
                    callback();
                }
            });
        },
        insert: function (forestTitle, tariff, forestImage, parentId, callback) {
            if (tariff > 0) {
                tariff = (new Number(tariff)).toFixed(2);
            }
            var paramData = {
                title: forestTitle,
                tariff: tariff,
                parentId: parentId,
                image: forestImage
            };
            $rootScope.ajaxRequestPost(service._baseUrl + "publishForest.htm", paramData, function (resultJson) {
                if (callback) {
                    callback();
                }
            });
        },
        updateForest: function (forest, callback) {
            delete forest.gmtCreate;
            delete forest.gmtModified;
            $rootScope.ajaxRequestPost(service._baseUrl + "updateForest.htm", forest, function (resultJson) {
                if (callback) {
                    callback();
                }
            });
        }
    };
    return service;
}]);
